package com.runda.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bbjob.dto.Result;
import com.bbjob.model.QueChoice;
import com.bbjob.model.QueOperation;
import com.bbjob.model.QuestionBank;
import com.runda.entity.JwtUser;
import com.runda.sec.SecContextUtil;
import com.runda.service.IQuestionBankService;
import com.runda.util.DataEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "questionbankApi", description = "题库管理")
@RestController
@RequestMapping(path = "questionbank", produces = "application/json;charset=utf-8")
public class QuestionBankController {

	@Resource
	private IQuestionBankService questionBankService;

	@ApiOperation(value = "查询题库", notes = "根据考试id查询题库列表")
	@RequestMapping(value = "getListByExamId/{id}", method = RequestMethod.GET)
	public Result getListByExamId(@PathVariable("id") Integer id) {

		List<QuestionBank> list = questionBankService.selectByExamId(id);

		return new Result(list);
	}

	@ApiOperation(value = "试卷同步", notes = "与客户端试卷同步")
	@RequestMapping(value = "syncBank", method = RequestMethod.POST)
	public Result syncBank(@RequestBody String json) {
		int ok = questionBankService.syncQuesbank(json);
		switch (ok) {
		case 1:
			return new Result(200, "同步成功");
		case -1:
			return new Result(8106, "数据无法解析");
		case -2:
			return new Result(8106, "保存试卷信息失败");
		case -3:
			return new Result(8106, "组织码不存在");
		case -4:
			return new Result(8106, "试卷已存在");
		case -5:
			return new Result(8106, "保存试卷失败，客观题保存失败");
		case -6:
			return new Result(8106, "保存试卷失败，主观题保存失败");
		default:
			return new Result(8106, "同步失败");
		}
	}

	/**
	 * 获取私有试卷(根据userid获取相应权限)
	 * 
	 * @param name
	 *            试卷名字过滤
	 * @param userid
	 *            用户id
	 * @param page
	 * @param size
	 * @return
	 */
	@ApiOperation(value = "获取私人试卷", notes = "获取私人试卷")
	@RequestMapping(value = "getBank", method = RequestMethod.GET)
	public DataEntity getBank(Integer type, String name, Integer userid,Integer difficulty,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int size) {
		return DataEntity.getInstance(questionBankService.getBank(type,name, difficulty,userid, page, size));
	}

	/**
	 * 获取公共试卷
	 * 
	 * @param name
	 *            试卷名字过滤
	 * @param userid
	 *            用户id
	 * @param page
	 * @param size
	 * @return
	 */
	@ApiOperation(value = "获取系统公共试卷", notes = "获取系统公共试卷")
	@RequestMapping(value = "getFreeBank", method = RequestMethod.GET)
	public DataEntity getFreeBank(Integer type, String name,Integer difficulty,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int size) {
		return DataEntity.getInstance(questionBankService.getFreeBank(type, name, difficulty, page, size));
	}

	/**
	 * 获取客观题私有题库(根据userid获取权限)
	 * 
	 * @param name
	 *            题干
	 * @param userid
	 *            用户id
	 * @param type
	 *            1选择2判断
	 * @param page
	 * @param size
	 * @return
	 */
	@ApiOperation(value = "获取客观题私有题库", notes = "获取主观题私有题库")
	@RequestMapping(value = "getChoice", method = RequestMethod.GET)
	public DataEntity getChoice(String bankid, String tag, String name, Integer type, Integer userid,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int size,Integer bankType) {
		return DataEntity.getInstance(questionBankService.getChoice(bankid, tag, name, type, userid, page, size,bankType));
	}

	/**
	 * 获取客观题公共题库(根据userid获取权限)
	 * 
	 * @param name
	 *            题干
	 * @param userid
	 *            用户id
	 * @param type
	 *            1选择2判断
	 * @param page
	 * @param size
	 * @return
	 */
	@ApiOperation(value = "获取客观题公共题库", notes = "获取主观题公共题库")
	@RequestMapping(value = "getFreeChoice", method = RequestMethod.GET)
	public DataEntity getFreeChoice(String bankid, String tag, String name, Integer type, Integer userid,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int size,Integer bankType) {
		return DataEntity.getInstance(questionBankService.getFreeChoice(bankid, tag, name, type, userid, page, size,bankType));
	}

	/**
	 * 获取主观题私有题库(根据userid获取权限)
	 * 
	 * @param name
	 *            题干
	 * @param type
	 *            题目类型
	 * @param userid
	 *            用户id
	 * @param page
	 * @param size
	 * @return
	 */
	@ApiOperation(value = "获取主观题私有题库", notes = "获取主观题公共题库")
	@RequestMapping(value = "getOperation", method = RequestMethod.GET)
	public DataEntity getOperation(String bankid, String tag, String name, Integer type, Integer userid,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int size) {
		return DataEntity.getInstance(questionBankService.getOperation(tag, bankid, name, type, userid, page, size));
	}

	/**
	 * 获取主观题公共题库(根据userid获取权限)
	 * 
	 * @param name
	 *            题干
	 * @param type
	 *            题目类型
	 * @param userid
	 *            用户id
	 * @param page
	 * @param size
	 * @return
	 */
	@ApiOperation(value = "获取主观题公共题库", notes = "获取主观题公共题库")
	@RequestMapping(value = "getFreeOperation", method = RequestMethod.GET)
	public DataEntity getFreeOperation(String bankid, String tag, String name, Integer type, Integer userid,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int size) {
		return DataEntity.getInstance(questionBankService.getFreeOperation(tag, bankid, name, type, userid, page, size));
	}

	/**
	 * 根据客观题ID获取详细信息
	 * 
	 * @param Choiceid
	 * @return
	 */
	@ApiOperation(value = "根据客观题ID获取详细信息", notes = "获取主观题私有题库")
	@RequestMapping(value = "getItemByChoiceid", method = RequestMethod.GET)
	public DataEntity getItemByChoiceid(String Choiceid) {
		return DataEntity.getInstance(questionBankService.getItemByChoiceid(Choiceid));
	}

	/**
	 * 根据试卷id获取客观题
	 * 
	 * @param Choiceid
	 * @return
	 */
	@ApiOperation(value = "根据试卷id获取客观题", notes = "根据试卷id获取客观题")
	@RequestMapping(value = "getChoiceByBank", method = RequestMethod.GET)
	public DataEntity getChoiceByBank(QueChoice choice, @RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {
		return DataEntity.getInstance(questionBankService.getChoiceByBank(choice, page, rows));
	}

	/**
	 * 根据试卷id获取主观题
	 * 
	 * @param operation
	 * @param page
	 * @param rows
	 * @return
	 */
	@ApiOperation(value = "根据试卷id获取主观题", notes = "根据试卷id获取主观题")
	@RequestMapping(value = "getOperationByBank", method = RequestMethod.GET)
	public DataEntity getOperationByBank(QueOperation operation,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {
		return DataEntity.getInstance(questionBankService.getOperationByBank(operation, page, rows));
	}

	/**
	 * 客观题目标记
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "客观题目标记", notes = "客观题目标记")
	@RequestMapping(value = "deleteChoice", method = RequestMethod.GET)
	public Result deleteChoice(@RequestParam(required = true) String id) {
		JwtUser user = SecContextUtil.getCurrentUser();
		int ok = questionBankService.deleteChoice(user.getId(), id);
		switch (ok) {
		case 1:
			return new Result(200, "删除成功");
		case -1:
			return new Result(500, "找不到题目!");
		default:
			return new Result(500, "删除失败!");
		}
	}

	/**
	 * 主观题目标记
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "主观题目标记", notes = "主观题目标记")
	@RequestMapping(value = "deleteOperation", method = RequestMethod.GET)
	public Result deleteOperation(@RequestParam(required = true) String id) {
		JwtUser user = SecContextUtil.getCurrentUser();
		int ok = questionBankService.deleteOperation(user.getId(), id);
		switch (ok) {
		case 1:
			return new Result(200, "删除成功");
		case -1:
			return new Result(500, "找不到题目!");
		default:
			return new Result(500, "删除失败!");
		}
	}

	/**
	 * 删除题库
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除题库", notes = "删除题库")
	// @Description(value = "type", notes = "0是不删除题目，1是删除题目")
	@RequestMapping(value = "deleteBank", method = RequestMethod.GET)
	public Result deleteBank(@RequestParam(required = true) String id) {
		JwtUser user = SecContextUtil.getCurrentUser();
		int ok = questionBankService.deleteBank(user.getId(), id);
		switch (ok) {
		case 1:
			return new Result(200, "删除成功");
		case -1:
			return new Result(500, "找不到试卷!");
		default:
			return new Result(500, "删除失败!");
		}
	}

	/**
	 * 根据试卷id获取已存在的所有题的id
	 */
	@ApiOperation(value = "根据试卷id获取已存在的所有题的id", notes = "根据试卷id获取已存在的所有题的id")
	@RequestMapping(value = "getAllQidByBid", method = RequestMethod.GET)
	public DataEntity getAllQidByBid(
			@ApiParam(name = "id", value = "试卷id，支持多个，用半角逗号隔开", required = true) @RequestParam(required = true) String id) {
		return DataEntity.getInstance(questionBankService.getAllUsefulQuestionByBank(id));
	}
	
	
	@ApiOperation(value = "分享题库为公有", notes = "分享题库为公有")
	// @Description(value = "type", notes = "0是不删除题目，1是删除题目")
	@RequestMapping(value = "shareBank", method = RequestMethod.GET)
	public Result shareBank(@RequestParam(required = true) String id) {
		JwtUser user = SecContextUtil.getCurrentUser();
		int ok = questionBankService.shareQuestionBank(user.getId(), id);
		switch (ok) {
		case 1:
			return new Result(200, "分享成功");
		case -1:
			return new Result(500, "找不到试卷!");
		default:
			return new Result(500, "分享失败!");
		}
	}
	
	@ApiOperation(value = "分享题目为公有", notes = "分享题目为公有")
	// @Description(value = "type", notes = "0是不删除题目，1是删除题目")
	@RequestMapping(value = "shareChoiceOrOperation", method = RequestMethod.GET)
	public Result shareChoiceOrOperation(@ApiParam(name = "type", value = "题目类型，1客观题2主观题", required = true)
		@RequestParam(required = true) Integer type,@RequestParam(required = true) String id) {
		JwtUser user = SecContextUtil.getCurrentUser();
		int ok = questionBankService.shareChoiceOrOperation(type, id, user.getId());
		switch (ok) {
		case 1:
			return new Result(200, "分享成功");
		case -1:
			return new Result(500, "类型错误!");
		case -2:
			return new Result(500, "找不到试题!");
		case -3:
			return new Result(500, "未指定试题!");
		default:
			return new Result(500, "分享失败!");
		}
	}
	
	/**
	 * 根据客观题id获取解析
	 * 
	 * @param operation
	 * @param page
	 * @param rows
	 * @return
	 */
	@ApiOperation(value = "根据客观题id获取解析", notes = "根据客观题id获取解析")
	@RequestMapping(value = "getChoiceAnalysis", method = RequestMethod.GET)
	public DataEntity getChoiceAnalysis(String id) {
		return DataEntity.getInstance(questionBankService.getChoiceAnalysis(id));
	}
	
	/**
	 * 根据主观题id获取解析
	 * 
	 * @param operation
	 * @param page
	 * @param rows
	 * @return
	 */
	@ApiOperation(value = "根据主观题id获取解析", notes = "根据主观题id获取解析")
	@RequestMapping(value = "getOperationAnalysis", method = RequestMethod.GET)
	public DataEntity getOperationAnalysis(String id) {
		return DataEntity.getInstance(questionBankService.getOperationAnalysis(id));
	}


}
